﻿//Demo
/*
	remoteData({
		root: 'photoWall1',		//图片墙位置('id') ，默认（'photoWall1'）
		row : 4,				//行，默认（5）
		line : 15,				//列，默认（20）
		timer : 10000,			//默认替换时间，默认（10秒）
		url : ""				//ajax地址，默认（'data/photoWallData.php'）
	});
*/
//通过js路径 插入其他文件路径
$.extend({
	includePath:"",
    include: function(file){
        var files = typeof file == "string" ? [file] : file;
        for (var i = 0; i < files.length; i++)
        {
            var name = files[i].replace(/^\s|\s$/g, "");
            var att = name.split('.');
            var ext = att[att.length - 1].toLowerCase();
            var isCSS = ext == "css";
            var tag = isCSS ? "link" : "script";
            var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
            var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
            if ($(tag + "[" + link + "]").length == 0) document.write("<" + tag + attr + link + "></" + tag + ">");
        }
    },
    includeJsPath: function(file){
		var scripts = document.getElementsByTagName('script');  
		var currentScript = scripts[scripts.length - 1];  
		var currentSrc = currentScript.getAttribute("src");
		var currentSrcLen = currentSrc.lastIndexOf("/");
		var src = currentSrc.substr(0,currentSrcLen+1);
		$.includePath = src;
		$.include(file);
	}
});

$.includeJsPath("jquery.easing.1.3.js");

function remoteData(config){
	var root = config.root || 'photoWall1';
	var row = config.row || 5;
	var line = config.line || 20;
	var timer = config.timer || 10000;
	var height = config.height || 49;
	var width = config.width || 49;
	var url = config.url || 'data/photoWallData.php';
	var romoteTime = '';
	var picWall = function (config){	//照片墙
		var data = config.data;			//json数据
		var left = config.left || 1;	//
		var root = $('#'+config.root);	//photowall位置
		var photoWallPosArr = [];		//photowall （行）数组
		var time = config.time || 3000;	//执行时间
		var Timer = '';					//定时器
		
		var insertPhoto = function(obj){
			var photoSize = obj['size'];
			var photoNum = obj['num'];
			var imgSrc = obj['src'];
			var liHtml = '';
			var insertFalg = false;
			
			for(var i = 0, j  = row; i < j; i ++){
				if(insertFalg){
					continue;
				}
				var liLeft = i * 50;
				outerloop:for(var m = 0, n  = line; m < n; m ++){
					var liTop = m *50;
					if(photoWallPosArr[i][m] == 0){
						
						if(photoSize == 'big'){
							if(m + 3 > n || i + 3 > j ){
								continue;
							}
							for(var L = i, k = i + 3; L < k; L++ ){
								for (var R = m, g = m + 3; R < g; R++ ){
									if(photoWallPosArr[L][R] != 0){
										continue outerloop;
									}
								}
							}
							
							for(var L = i, k = i + 3; L < k; L++ ){
								for (var R = m, g = m + 3; R < g; R++ ){
										photoWallPosArr[L][R] = photoNum;
								}
							}
						}else{
							photoWallPosArr[i][m] = photoNum;
						}
						var liHeight = height + 'px';
						var liWidth = width + 'px';
						if(photoSize){
							liHeight = (height*3+2) + 'px';
							liWidth = (width*3+2) + 'px';
						}
						//var liHtml = '<li style="height:'+liHeight+';width:'+liWidth+';background:#Faa;position:absolute;left:' + liTop + 'px;top:' + liLeft + 'px;">'+photoNum+'</li>';
						
						var liHtml = '<li style="height:'+liHeight+';width:'+liWidth+';background:#Faa;position:absolute;left:' + liTop + 'px;top:' + liLeft + 'px;"><img src="'+imgSrc+'" style="height:'+liHeight+';width:'+liWidth+';" /></li>';
											
						//if(m == (n - 1) && i != j-1){		//插入<br />每行行尾（不包括最后一行）
						//	return liHtml+'<br />'
						//}
						return liHtml;
					}				
				}
			}
			return null;
		};
		//var debug = '';								//调试
		for(var i = 0, j  = row; i < j; i ++){
			photoWallPosArr[i] = [];				//photowall （列）数组
			for(var m = 0, n  = line; m < n; m ++){
				photoWallPosArr[i][m] = 0;
				//debug += photoWallPosArr[i][m] + ",";
			}
			//debug += '<br />';
		}
		//$('#demo').append(debug);
		
		var picWallHtml = '';
		if(left == -1){
			picWallHtml = '<ul style="height:' + (row*50) + 'px; top:0; width:' + (line*50) + 'px; position: absolute; left:'+(-line*50)+'px;" >';
		}else{
			picWallHtml = '<ul style="height:' + (row*50) + 'px; top:0; width:' + (line*50) + 'px; position: absolute;" >';
		}
		
		//var picWallHtml = '<ul style="height:'+this.row*50+'px; width:'+this.line*50+' position: absolute;" >';
		for(var i = 0, j = data.length; i < j; i ++){
			var backHtml = insertPhoto(data[i]);
			if(!backHtml){
				continue;
			}
			picWallHtml += backHtml;
		}
		picWallHtml += '</ul>';
		root.append(picWallHtml);
		//console.dir(photoWallPosArr);
	};
	
	var remoteDataQue = function(config){//setTimeout//setInterval
		var root = config.root;
		var row = config.row;
		var line = config.line;
		var time = '';
		$.ajax({
			url : url,
			//cache : false,
			dataType : 'json',
			error : function(){
			},
			success : function(data) {
				var rootId = root;
				picWall({'root': root, 'data' : data, left : -1});
				if($('#' + rootId).children().length > 2){
					$('#' + rootId).children().eq(0).remove();
				}
				var $photoWall1Ul = $('#' + rootId + '>ul:last-child');
				$photoWall1Ul.animate({ 
					left : '0px'
				}, 1000, 'easeInCirc');
			}
		});	
		romoteTime = setTimeout(function(){
			remoteDataQue(config);	
		},timer);
	}
	
	$.ajax({
		url : url,
		//cache : false,
		dataType : 'json',
		error : function(){
		},
		success : function(data) {
			picWall({'root': root, 'data' : data});
		}
	});
	//romoteTime = setInterval(function(){
	//	remoteDataQue(config);	
	//},timer);
	remoteDataQue(config);
	
	$('#'+root).hover(
	function(){
		clearTimeout(romoteTime);//clearInterval//clearTimeout
	},
	function(){
		romoteTime = setTimeout(function(){//setInterval//setTimeout
			remoteDataQue(config);	
		},timer);	
	});	
};